package com.example.utils.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

/**
 *  TODO  文件读取 MD5 码
 * @Author 江
 * @Date 2021/10/13 下午 16:04
 */
public class MD5Check {
    public static String getFileMD5(String filePath) throws Exception{
        File file = new File(filePath);
        InputStream in = new FileInputStream(file);
        MessageDigest digest = MessageDigest.getInstance("MD5");  ;
        byte buffer[] = new byte[1024];
        int len;
        while((len = in.read(buffer))!=-1){
            digest.update(buffer, 0, len);
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        return bigInt.toString(16);
    }

    public static void main(String[] args) throws Throwable{
        String f1 = getFileMD5("C:\\Users\\xiaojiang\\Downloads\\gitlab-ce-13.0.12-ce.0.el6.x86_64.rpm");
        System.out.println(f1);
        System.out.println(f1.equals("2afc8e3f4c0ae573204341f25717f7d8"));
    }

}
